<?php
global $_W, $_GPC;
$uid = $_GPC['uid'];
$sonid = $_GPC['sonid'];
$lessonid = $_GPC['lid'];
$answer = json_decode(htmlspecialchars_decode($_GPC['allAnswer']), true);
$allAnswer = [];
foreach ($answer as $key => &$val) {
    $allAnswer[$val['id']] = $val;
}
if (empty($uid)) {
    return $this->getresult(1, '请先登录', array('code' => 3001, 'msg' => '请先登录'));
}
if (empty($lessonid)) {
    return $this->getresult(1, '请先选择课程', array('code' => 3001, 'msg' => '请先选择课程'));
}
/***校验token开始***/
if ($uid) {
    $token = $_GPC['token'];
    if (empty($token)) {
        return $this->getresult(1, '请重新登录', ['code' => 4001, 'msg' => '请重新登录']);
    }
    $token_result = $this->checktoken($uid, $token);
    if ($token_result == 1002) {
        return $this->getresult(1, '请重新登录', ['code' => 4002, 'msg' => '请重新登录']);
    } elseif ($token_result == 1003) {
        return $this->getresult(1, '请重新登录', ['code' => 4003, 'msg' => '请重新登录']);
    } elseif ($token_result == 1001) {
        //  更新token
        pdo_update($this->t_user_token, array('createtime' => time()), array('uid' => $uid, 'token' => $token, 'weid' => $_W['uniacid']));
    }
}
/***校验token结束***/
$types_config = array('1' => '判断题', '2' => '单选题', '3' => '多选题', '4' => '填空题');
$weid = $_W['uniacid'];
if (empty($sonid)) {
    return $this->getresult(1, '章节id未获取到', array('code' => 3001, 'msg' => '章节id未获取到'));
}
$params = [':weid' => $weid, ':sonid' => $sonid, ':lessonid' => $lessonid, ':status' => 1];
$practiceList = pdo_fetchall("select * from " . tablename('goouc_class_practice') . "  where weid = :weid AND sonid = :sonid AND status = :status AND lessonid = :lessonid", $params);
$num = 0;//答对的提数
foreach ($practiceList as &$v) {
    if ($v['answer'] == $allAnswer[$v['id']]['answer']) {
        $data['data'][] = ['status' => 1];
        $rdata[$v['id']] = ['id' => $v['id'], 'status' => 1, 'uanswer' => $allAnswer[$v['id']]['answer']];
        $num++;
    } else {
        $data['data'][] = ['status' => 0];
        $rdata[$v['id']] = ['id' => $v['id'], 'status' => 0, 'uanswer' => $allAnswer[$v['id']]['answer']];
        //先判断是不是存在
        $paramsfalse = array('weid' => $weid, 'uid' => $uid, 'sonid' => $sonid, 'lessonid' => $lessonid, 'practiceid' => $v['id']);
        $checkfalse = pdo_get('goouc_class_practice_false_record', $paramsfalse, array('id', 'is_delete'));
        //保存错题记录
        $falseInsert = [
            'weid' => $weid,
            'uid' => $uid,
            'lessonid' => $lessonid,
            'sonid' => $sonid,
            'practiceid' => $v['id'],
            'answer' => $v['answer'],
            'useranswer' => $allAnswer[$v['id']]['answer'],
            'createtime' => time()
        ];
        if (!$checkfalse) {
            pdo_insert('goouc_class_practice_false_record', $falseInsert);
        } else {
            if ($checkfalse['is_delete'] == 2) {
                $falseInsert['is_delete'] = 1;
            }
            pdo_update('goouc_class_practice_false_record', $falseInsert, array('id' => $checkfalse['id']));
        }

    }
}
//判断这个学生有没做过这个章节的习题
$checkR = pdo_get('goouc_class_practice_record', array('weid' => $weid, 'uid' => $uid, 'lessonid' => $lessonid, 'sonid' => $sonid, 'status' => 1), array('id'));
//保存做题记录
if (!$checkR['id']) {
    $recordInsert = [
        'weid' => $weid,
        'uid' => $uid,
        'lessonid' => $lessonid,
        'sonid' => $sonid,
        'record' => serialize($rdata),
        'createtime' => time(),
        'updatetime' => time()
    ];
    pdo_insert('goouc_class_practice_record', $recordInsert);
} else {
    //之前每次做题产生一条新的记录，这次改为，只有一条记录（为了这个原因），把原来的记录标记为删除状态
    $paramR = array('weid' => $weid, 'uid' => $uid, 'lessonid' => $lessonid, 'sonid' => $sonid, 'status' => 1, ':id' => $checkR['id']);
    $checkrelist = pdo_fetchall('select id from ' . tablename('goouc_class_practice_record') . ' where weid = :weid AND uid = :uid AND lessonid = :lessonid AND sonid = :sonid AND status = :status AND id <> :id', $paramR);
    if (!empty($checkrelist)) {
        foreach ($checkrelist as $k => $v) {
            pdo_update('goouc_class_practice_record', array('status' => 2), array('id' => $v['id']));
        }
    }
    $recordUpdate = [
        'record' => serialize($rdata),
        'updatetime' => time()
    ];
    pdo_update('goouc_class_practice_record', $recordUpdate, array('id' => $checkR['id']));
}

//第一次做题统计星星(只统记录第一次获取星星的数量)
$paramsFirst = array('weid' => $weid, 'uid' => $uid, 'sonid' => $sonid, 'lessonid' => $lessonid, 'status' => 1);
$checkFirst = pdo_get('goouc_class_user_star', $paramsFirst, array('id', 'num'));
if (!$checkFirst) {
    $starInsert = [
        'weid' => $weid,
        'uid' => $uid,
        'lessonid' => $lessonid,
        'sonid' => $sonid,
        'createtime' => time()
    ];
    $rightPercent = floor(($num / count($practiceList)) * 100) / 100;
    if ($rightPercent == 1) {
        $starNum = 3;
    } elseif (0.5 <= $rightPercent && $rightPercent < 1) {
        $starNum = 2;
    } elseif (0 < $rightPercent && $rightPercent < 0.5) {
        $starNum = 1;
    } elseif ($rightPercent == 0) {
        $starNum = 0;
    }
    $starInsert['num'] = $starNum;
    pdo_insert('goouc_class_user_star', $starInsert);
    $data['star'] = $starNum;
} else {
    $data['star'] = $checkFirst['num'];
}
return $this->getresult(0, '闯关成功', ['code' => 200, 'data' => $data]);